from bddjango import APIView, APIResponse, my_api_assert_function, get_list, Time
from . import models
from bddjango import CompleteModelView
from bddjango import BaseListView
from bdtime import show_ls, show_json
from bdtime import tt
from bddjango import conv_qs_ls_to_serializer_data
from . import serializers


class Test(APIView):
    def get(self, request, *args, **kwargs):
        ret = 'hello, world!'
        return APIResponse(ret, msg='ok', status=200)


class Email(CompleteModelView):
    """
    Email

    POST /api/index/Email/    # 增
    {
        "biao_ti": "xxx",
        "fa_jian_ren": "xxx"
    }
    DELETE /api/index/Email/1/    # 删
    PUT /api/index/Email/1/      # 改, 携带数据的格式和POST一样

    GET /api/index/Email/    # 查, 列表页
    GET /api/index/Email/1/    # 查, 详情页
    """
    default_page_size = 99
    queryset = models.Email
    serializer_class = serializers.Email

    def get_queryset(self):
        # qs_ls = super().get_queryset()
        t_now = tt.get_current_beijing_time_str(decimal_places=0)
        qs_ls = models.Email.objects.filter(fa_song_shi_jian__lte=t_now, guo_qi_shi_jian__gte=t_now)
        return qs_ls

    # def get(self, requests, *args, **kwargs):
    #     # current_time = django
    #     t_now = tt.get_current_beijing_time_str(decimal_places=0)
    #     # models.Email.objects.filter(guo_qi_shi_jian__gte=t_now)
    #     # models.Email.objects.filter(guo_qi_shi_jian__lte=t_now)
    #     qs_ls = models.Email.objects.filter(fa_song_shi_jian__lte=t_now, guo_qi_shi_jian__gte=t_now)
    #     _dc_ls = conv_qs_ls_to_serializer_data(qs_ls)
    #     dc_ls = []
    #     for _dc_i in _dc_ls:
    #         # print(_dc_i)
    #
    #         _id = _dc_i.get('id')
    #         fu_jian_id = _dc_i.get('fu_jian_id').replace('；', ';')
    #         fu_jian_shu_liang = _dc_i.get('fu_jian_shu_liang').replace('；', ';')
    #         try:
    #             id_ls = fu_jian_id.split(';')
    #             num_ls = fu_jian_shu_liang.split(';')
    #             assert len(id_ls) == len(num_ls), '`id_ls`和`num_ls`的数量不相等!'
    #             fu_jian_dc_ls = []
    #             for id_i, num_i in zip(id_ls, num_ls):
    #                 fu_jian_dc_ls.append(
    #                     {
    #                         'id': id_i,
    #                         'num': int(num_i),
    #                     }
    #                 )
    #
    #             # fu_jian_ls = list(zip(fu_jian_id.split(';'), fu_jian_shu_liang.split(';')))
    #         except Exception as e:
    #             raise ValueError(f"ID[{_id}]提取`fu_jian_ls`时出现错误, 可能是`;`号数量不相等? error: {e}")
    #
    #         guo_qi_shi_jian = _dc_i.get('guo_qi_shi_jian')
    #         # from tools.time_utils import get_seconds_diff, format_duration
    #         # remain_seconds = get_seconds_diff(guo_qi_shi_jian, t_now)
    #         # remain_duration = format_duration(remain_seconds, length=2)
    #
    #         # remain_time = 1
    #         # f"{123:g}{'asdf'}"
    #
    #         dc_i = {
    #             'id': _id,
    #             'biao_ti': _dc_i.get('biao_ti'),
    #             'fa_jian_ren': _dc_i.get('fa_jian_ren'),
    #             'nei_rong': _dc_i.get('nei_rong'),
    #             'guo_qi_shi_jian': guo_qi_shi_jian,
    #             # 'remain_duration': remain_duration,
    #             'fu_jian_dc_ls': fu_jian_dc_ls,
    #         }
    #         dc_ls.append(dc_i)
    #
    #     # show_json(dc_ls)
    #     total_length = len(dc_ls)
    #
    #     # ret = {
    #     #     'page_dc': {
    #     #         'count_items': total_length,
    #     #         'total_pages': total_length,
    #     #         'page_size': total_length,
    #     #         'p': 1,
    #     #     },
    #     #     'data': dc_ls,
    #     # }
    #     ret = dc_ls
    #     return APIResponse(ret, status=200, msg='ok')
